//获取应用实例
const app = getApp();
import httpsUtils from '../../utils/https.js';

var rate = 0; //分辨转换
var floatTop = 0; //悬浮高度
Page({
  data: {
    bannerList: [],
    indicatorDots: true,
    circular: true,
    list: [],
    list_total: 0,
    indexList: [],
    // small_nav初始数据
    tabs: [
      { id: "nav_1", isSelect: true, title: "商品", value: 1, isFirst: false },
      { id: "nav_2", isSelect: false, title: "店铺", value: 2, isFirst: true },
    ], //tabbar数组
    curTabId: "nav_1", //当前tabid
    isShowFloatTab: false, //是否显示悬浮tab
    guessLikeList: [], //商品列表
    guessLikeLenght: 0,
    guessCurrent: 1,
    guessHasNextPage: true,

    newsList: [], //店铺列表
    newslength: 0,
    newsCurrent: 1,
    newsHasNextPage: true,
  },
  /**
  * 获取搜索店铺，或者商品
  */
  getGoodsOrShop: function (e) {
    var that = this;

    var nowTab = 1;
    for (let obj of that.data.tabs) {
      if (obj.isSelect) {
        nowTab = obj.value;
      }
    }

    var current = 1;
    var searchType = '';
    //商品
    if (nowTab == 1) {
      current = that.data.guessCurrent;
    }
    //店铺
    if (nowTab == 2) {
      current = that.data.newsCurrent;
      searchType = 1;
    }

    var searchLog = [];
    searchLog = wx.getStorageSync("searchKey");

    httpsUtils.getData({
      _url: app.globalData._url + '/goods/search2?current=' + current + "&types=" + searchType + "&goodsname=" + searchLog[0],
      data: '',
    }, function (json) {
      if (json.success) {
        //商品
        if (nowTab == 1) {
          that.setData({
            guessLikeList: that.data.guessLikeList.concat(json.data.list),
            guessLikeLenght: json.data.total,
            guessCurrent: json.data.pageNum,
            guessHasNextPage: json.data.hasNextPage,
          })
        }
        //店铺
        if (nowTab == 2) {
          that.setData({
            newsList: that.data.newsList.concat(json.data.list),
            newsLength: json.data.total,
            newsCurrent: json.data.pageNum,
            newsHasNextPage: json.data.hasNextPage,
          })
        }
      }
    })
  },
  /**
  * 页面上拉触底事件的处理函数
  */
  onReachBottom: function () {
    var that = this;
    
  },
  
  /**
   * 下拉刷新
   */
  goodPullDown: function () {
    var that = this;
    var nowTab = 1;
    for (let obj of that.data.tabs) {
      if (obj.isSelect) {
        nowTab = obj.value;
      }
    }
    //推荐
    if (nowTab == 1) {
      if (that.data.guessHasNextPage) {
        that.setData({
          guessCurrent: that.data.guessCurrent + 1,
        })
        that.getGoodsOrShop();
      }
    }
    //店铺
    if (nowTab == 2) {
      if (that.data.newsHasNextPage) {
        that.setData({
          newsCurrent: that.data.newsCurrent + 1,
        })
        that.getGoodsOrShop();
      }
    }
  },
  /**
     * 生命周期函数--监听页面加载
     */
  onLoad: function (options) {
    //获取商品列表
    this.getGoodsOrShop();
  },
  //事件函数
  //search 搜索
  searchs: function () {
    wx.navigateTo({
      url: '../search/search',
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.goodPullDown();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

 
 

  /*small nav*/
  /**
   * 获得滑动导致悬浮开始的高度
   * @return {[type]} [description]
   */
  getScrollTop: function () {
    var that = this;
    if (wx.canIUse('getSystemInfo.success.screenWidth')) {
      wx: wx.getSystemInfo({
        success: function (res) {
          rate = res.screenWidth / 750;
          floatTop = 2458 * rate;
          that.setData({
            scrollTop: 2458 * res.screenWidth / 750,
            scrollHeight: res.screenHeight / (res.screenWidth / 750) - 128,
          });
        }
      });
    }
  },

  /**
    * 生命周期函数--监听页面加载
    */
  onPageScroll: function (event) {
    var scrollTop = event.scrollTop;
    if (scrollTop >= floatTop && !this.data.isShowFloatTab) {
      this.setData({
        isShowFloatTab: true,
      });
    } else if (scrollTop < floatTop && this.data.isShowFloatTab) {
      this.setData({
        isShowFloatTab: false,
      });
    }
  },
  /**
     * 点击tab切换
     * @param  {[type]} event 
     * @return {[type]}       
     */
  clickTab: function (event) {
    var id = event.detail.id;
    this.data.curTabId = id;
    var isFirst = true;
    for (var i = 0; i < this.data.tabs.length; i++) {
      if (id == this.data.tabs[i].id) {
        isFirst = this.data.tabs[i].isFirst
        this.data.tabs[i].isSelect = true;
        this.data.tabs[i].isFirst = false;
      } else {
        this.data.tabs[i].isSelect = false;
      }
    }
    this.setData({
      tabs: this.data.tabs,
      curTabId: this.data.curTabId,
    });

    //更新数据，第一次点击时候加载重新加载数据
    if (isFirst) {
      this.getGoodsOrShop();
    }
  },
  
})

